import pymysql as pymysql

# ---------连接--------------
if __name__ == '__main__':
    connect = pymysql.connect(host='localhost',   # 本地数据库
                              user='root',
                              password='root',
                              db='ry',
                              charset='utf8') #服务器名,账户,密码，数据库名称
    try:
        cur = connect.cursor()
        cur.execute('set names utf8')
        cur.execute('set autocommit=1')
        sql = "insert into address(name, address)values('张三', '湖北张家口')"
        # 执行sql并且得到结果
        cur.execute(sql)
        # 关闭游标和连接
        cur.close()
        connect.close()
    except Exception as e:
        print("插入数据失败:", e)
    else:
        print("插入数据成功;")